package com.corpdata.activiti.controller;

import javax.servlet.http.HttpServletRequest;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.corpdata.activiti.entity.ProcessInfo;
import com.corpdata.activiti.service.ProcessService;
import com.corpdata.common.base.BaseController;
import com.corpdata.common.result.DataTable;
import com.corpdata.common.result.Result;
import com.corpdata.entity.OrgUser;

/**
 * 
 * @author zealon
 * @date 2018年4月18日
 * 
 * 参考：https://gitee.com/jamen/slife/blob/master/slife-activiti/src/main/java/com.slife/controller/ProcessController.java
 */
@Controller
@RequestMapping("/process")
public class ProcessController extends BaseController {

	@Autowired
	private ProcessService processService;

	//流程中心主页
	@GetMapping(value = "")
	public String index(ModelMap map, HttpServletRequest request) {
		OrgUser user = (OrgUser)request.getSession().getAttribute("userinfo");
		if(user==null){
			return "login";
		}else{
			map.addAttribute("userName", user.getUserid());
			return "workflow/index";
		}
	}
	
	@GetMapping(value = "/list")
	public String list(org.springframework.ui.Model model, HttpServletRequest request) {
		model.addAttribute("url", request.getContextPath()+"/process/list");
		return "workflow/process/process_list";
	}
	
	@ResponseBody
	@PostMapping(value = "/list/data")
	public DataTable<ProcessInfo> getListData(DataTable dt) {
		if (null == dt) {
            return null;
        }
		return processService.getProcessByPage(dt);
	}
	
	@ResponseBody
	@PostMapping(value = "/start")
	public Result startProcessInstance(String key,HttpServletRequest request){
		String userId = ((OrgUser)request.getSession().getAttribute("userinfo")).getUserid();
		return processService.startProcessInstance(key,userId);
	}
	
	@GetMapping(value ="/instance/list")
	public String getProcessInstanceList(){
		return "workflow/instance/instance_list";
	}
	
	@ResponseBody
	@PostMapping(value = "/instance/list/data")
	public DataTable<ProcessInstance> getProcessInstanceListData(DataTable dt) {
		if (null == dt) {
            return null;
        }
		return processService.getProcessInstanceByUser(dt);
	}
	
	//用户任务列表
	@GetMapping(value ="/task/list")
	public String getTaskByUser(){
		return "workflow/instance/task_list";
	}
	
	@ResponseBody
	@PostMapping(value = "/task/list/data")
	public DataTable<Task> getTaskDataByUser(DataTable dt, HttpServletRequest request) {
		if (null == dt) {
            return null;
        }
		String userId = ((OrgUser)request.getSession().getAttribute("userinfo")).getUserid();
		return processService.getTaskByUser(dt,userId);
	}
	
	//执行流转流程
	@ResponseBody
	@PostMapping(value = "/task/complete")
	public Result completeTask(String taskId) {
		if (null == taskId) {
            return null;
        }
		return processService.completeTask(taskId);
	}
	
	//部署流程
	@GetMapping(value = "/deployed")
	@ResponseBody
	public String deployed(String taskId) {
		return processService.deployed();
	}
}
